<template>
	<view>
		<view class="">
			<!-- 2.0.19支持autoBack，默认为false -->
			<u-navbar :autoBack="true">
			</u-navbar>
		</view>

		<view class="con-body" style="background-color: #FEFEFE;">
			<view class="uni-flex-center">
				<view class="uni-cell-90">
					<u-cell :icon="shop.avatar"
						:iconStyle="{width: '100rpx', height: '100rpx', borderRadius: '10rpx', marginRight: '20rpx'}"
						:label="'销量：' + shop.sales + '  好评：' + shop.good">
						<view slot="title">{{shop.nickname}}<text v-if="shop.type == 1"
								style="background-color: #f5deb3; color: #8b4513; font-size: 22rpx; padding: 2rpx 4rpx; border-radius: 6rpx; margin-left: 10rpx;">会员</text>
							<text v-if="shop.type == 2"
								style="background-color: #f5deb3; color: #8b4513; font-size: 22rpx; padding: 2rpx 4rpx; border-radius: 6rpx; margin-left: 10rpx;">旗舰店</text>
						</view>

					</u-cell>

					<u-tabs :list="shop.tabs" @click="click" @change="chargeTabs"></u-tabs>
					<view v-if="currentTab == 0">
						<view style="width: 100%; float: left; height: 65rpx; background-color: #f9f9f9;">
							<u-subsection :list="tabList" :current="currentSub" activeColor="#f56c6c"
								@change="TabChange" bgColor="#f9f9f9"></u-subsection>
						</view>
						<view class="uni-flex-center">
							<u-grid :border="false" col="2">
								<u-grid-item v-for="(item,index) in goodsList" :key="index"
									:customStyle="{width: '46%', marginLeft: '2%', marginRight:'2%',}">
									<view class="main-service" style="min-width: 100%; overflow: hidden;"
										@click="goodDetail(item.id, item.sid)">
										<u-image :src="item.icon" :lazy-load="true" width="98%" height="300rpx"
											:customStyle="{marginTop: '10rpx', marginLeft:'2%', borderRadius:'20rpx'}"
											mode="scaleToFill"></u-image>
										<view>
											<view style="margin-top: 10rpx; padding: 0rpx 10rpx;">
												<text style="font-size: 30rpx;">{{item.name}}</text>
											</view>
											<view style="margin-top: 2rpx;">
												<view style="display: inline-flex; text-overflow: ellipsis;">
													<text class="u-line-1"
														style="color:#E35719; display: flex; margin-left: 15rpx; font-size: 20rpx;">{{item.marks}}</text>
												</view>
											</view>
											<view style="margin-top: 3rpx;">
												<view style="display: inline-flex; text-overflow: ellipsis;">
													<text class="u-line-1"
														style="color:#999999; display: flex; margin-left: 15rpx; font-size: 22rpx;">{{item.remark}}</text>
												</view>
											</view>
											<text
												style=" color: #E35719; font-size: 20rpx; margin-left: 5rpx;  padding: 0 5rpx;border: 1rpx solid #E35719; border-radius: 6rpx;">售{{item.sales}}</text>
											<text
												style=" color: forestgreen; font-size: 20rpx; margin-left: 5rpx; padding: 0 5rpx; border: 1rpx solid forestgreen; border-radius: 6rpx;">评{{item.good}}</text>
											<text
												style=" color: #999; font-size: 20rpx; margin-left: 5rpx; padding: 0 5rpx; border: 1rpx solid #999; border-radius: 6rpx;">存{{item.stock}}</text>
											<view style="margin: 10rpx 0rpx;">
												<u-line dashed color="#999999"></u-line>
											</view>
											<view>
												<view style=" width: 100%;">
													<text
														style="float: left; color: red; font-size: 36rpx; font-weight: 800;">￥{{item.price}}</text>
													<text
														style="float: left; color: #9e9e9e; font-size: 22rpx; text-decoration: line-through; margin-left: 10rpx;">￥{{item.oriPrice}}</text>
												</view>
											</view>
										</view>
									</view>

								</u-grid-item>
							</u-grid>
						</view>
						
						<u-loadmore :status="status" />
						<u-empty :show="showEmpty" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png">
						</u-empty>
					</view>
					<view v-if="currentTab == 1">
						<view class="u-menu-wrap">
							<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view"
								:scroll-top="scrollTop">
								<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
									:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
									@tap.stop="swichMenu(index)">
									<text class="u-line-1">{{item.name}}</text>
								</view>
							</scroll-view>
							<block v-for="(item,index) in tabbar" :key="index">
								<scroll-view scroll-y class="right-box" v-if="current==index">
									<view class="page-view">
										<view class="class-item">
											<view class="item-title">
												<text>{{item.name}}</text>
											</view>
											<view class="item-container">
												<view class="thumb-box" v-for="(item1, index1) in item.cates"
													:key="index1">
													<image class="item-menu-image" :src="item1.icon" mode=""
														@click="gotoNavList(item1)"></image>
													<view class="item-menu-name">{{item1.name}}</view>
												</view>
											</view>
										</view>
									</view>
								</scroll-view>
							</block>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getShopDetail,
		getShopGoodsList
	} from "@/service/shopservice.js";
	export default {
		data() {
			return {
				sid: 0,
				shop: {},
				currentTab: 0,
				currentSub: 0,
				tabbar: [],
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				tabList: [
					'综合排序', '销量优先', '好评优先', '最新产品'
				],
				goodsList: [],
				limit: 10,
				page: 1,
				listMode: 'list',
				status: 'loadmore',
				showEmpty: false,
				shopName: ''
			}
		},
		onLoad(options) {
			this.sid = options.sid;
			console.log(this.sid);
			getShopDetail({
				sid: this.sid
			}).then(this.getShopDetail);
			getShopGoodsList({
				sid: this.sid
			}).then(this.getGoodsList);
		},
		onReachBottom() {
			if (this.status == 'nomore') return;
			this.status = 'loading';
			this.page = ++this.page;
			getShopGoodsList({
				sid: this.sid,
				sort: this.currentSub,
				page: this.page,
				limit: this.limit
			}).then(this.getGoodsList);
		},
		methods: {
			getShopDetail(e) {
				if (e.code == 200) {
					this.shop = e.result;
					this.tabbar = e.result.nav;
					this.shopName = e.result.nickname;
				}
			},
			chargeTabs(e) {
				this.currentTab = e.index;
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			TabChange(index) {
				this.currentSub = index;
				this.status = 'loadmore';
				this.page = 1;
				this.goodsList = [];
				getShopGoodsList({
					sid: this.sid,
					sort: this.currentSub,
					page: this.page,
					limit: this.limit
				}).then(this.getGoodsList);
			},
			getGoodsList(e) {
				if (e.code == 200) {
					this.showEmpty = false;
					let list = e.result;
					list.forEach(item => {
						this.goodsList.push(item)
					})
					if (list.length < this.limit) this.status = 'nomore';
				} else {
					this.status = 'nomore';
					if (this.goodsList.length == 0) {
						this.showEmpty = true;
					}
				}
			},
			goodDetail(id, sid) {
				this.$u.route({
					url: '/pagesShop/nav/good-detail',
					params: {
						gid: id,
						sid: sid
					}
				})
			},
			gotoNavList(item) {
				this.$u.route({
					url: "/pagesShop/nav/good-list",
					params: {
						sid: this.sid,
						title: item.name,
						icon: item.icon,
						cid: item.id,
						nickname: this.shopName
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid red;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 33.333333%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}

	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}
</style>
